package com.siruier.boss.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import com.siruier.boss.databinding.ViewNumberAddReduceBinding
import com.siruier.boss.ui.ext.bindViewClick


class NumberAddReduceView(
    context: Context,
    attrs: AttributeSet?
) : androidx.constraintlayout.widget.ConstraintLayout(context, attrs),
    View.OnClickListener {
    private val vb: ViewNumberAddReduceBinding = ViewNumberAddReduceBinding.inflate(LayoutInflater.from(context), this, true)
    var minNumber = 0
    private var listener: ((Int) -> Unit)? = null
    var maxNumber: Int = -1

    init {
        bindViewClick(this, vb.tvNumberReduce, vb.tvNumberAdd)
    }

    var enableAdd: Boolean = true
    var number: Int = minNumber
        set(value) {
            field = value
            changeNumber()
        }

    override fun onClick(v: View) {
//        if (Utils.notRepeatClick()) {
        when (v) {
            vb.tvNumberReduce -> {
                if (number > minNumber) {
                    number--
                    changeNumber()
                    listener?.invoke(number)
                }
            }
            vb.tvNumberAdd -> {
                if (enableAdd && (maxNumber == -1 || number < maxNumber)) {
                    number++
                    changeNumber()
                    listener?.invoke(number)
                }
            }
//            }
        }
    }

    private fun changeNumber() {
        vb.tvNumber.text = number.toString()
    }

    fun setNumberChangeListener(listener: ((Int) -> Unit)? = null) {
        this.listener = listener
    }
}